/*
 * Copyright (C) 2013  Richard Schilling. All rights reserved.
 * contact: coderroadie@gmail.com
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package android.persist.op.sqlite;

import static android.persist.op.sqlite.SQLLogging.SQL_LOG_TAG;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.persist.config.ClassConfig;
import android.persist.config.FieldConfig;
import android.persist.op.CreateStowage;
import android.persist.op.OpException;
import android.util.Log;

/**
 * {@code SQLiteTruncateStowage} applies a SQL "TRUNCATE" operation against the
 * underlying database to delete all the data associated with a specific class.
 * 
 * @author Richard Schilling
 * @param <OT> the type of class that this operation can function on.
 * @since 1.0
 */
public class SQLiteTruncateStowage<OT> extends CreateStowage<OT> {

    /**
     * The database that this operation is applied against.
     * 
     * @since 1.0
     */
    private final SQLiteDatabase mDatabase;

    /**
     * Class configuration object for {@link operand}'s type.
     * 
     * @since 1.0
     */
    private final ClassConfig<OT> mConfig;

    /**
     * Create a new truncate operation.
     * 
     * @param db the database to operate on.
     * @param c the class to truncate data for
     * @since 1.0
     */
    public SQLiteTruncateStowage(SQLiteDatabase db, Class<OT> c) {
        super(c);
        if (db == null) {
            throw new IllegalArgumentException("db cannot be null");
        }

        if (db.isReadOnly()) {
            throw new OpException("db is read only");
        }

        if (c == null) {
            throw new IllegalArgumentException("o cannot be null");
        }

        mDatabase = db;
        mConfig = (ClassConfig<OT>) ClassConfig.getConfig(operand);

    }

    /**
     * Executes the truncate operation.
     * 
     * @throws OpException if the database is closed
     * @throws SQLiteException if the truncate operation fails.
     * @since 1.0
     */
    @Override
    public Boolean call() {
        if (!mDatabase.isOpen())
            throw new OpException("database is closed");

        for (FieldConfig fConfig : mConfig.foreignFields) {
            if (!fConfig.isCollection()) {
                continue;
            }

            StringBuilder query = new StringBuilder();
            query.append("DELETE FROM '");
            query.append(mConfig.storageName);
            query.append("_");
            query.append(fConfig.getPersistedName());
            query.append("'");

            if (Log.isLoggable(SQL_LOG_TAG, Log.DEBUG)) {
                Log.d(SQL_LOG_TAG,
                        query.toString());
            }
            mDatabase.execSQL(query.toString());
        }

        StringBuilder query = new StringBuilder();
        query.append("DELETE FROM '");
        query.append(mConfig.storageName);
        query.append("'");

        mDatabase.execSQL(query.toString());

        return true;

    }

}
